package com.igeek.boot_4_druid.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeek.boot_4_druid.entity.User;
import com.igeek.boot_4_druid.entity.UserExample;
import com.igeek.boot_4_druid.mapper.UserMapper;
import com.igeek.boot_4_druid.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhiyu
 * @Date:11:35
 * @DESC:
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public boolean insertUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public boolean updatePic(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public boolean deleteOne(int id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<User> queryByName(String name) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameLike("%"+name+"%");
        List<User> userList = userMapper.selectByExample(userExample);
        return userList;
    }

    @Override
    public PageInfo<User> selectListByPage(int current, String name) {
        //开启分页逻辑
        //PageHelper.startPage(当前页码，每页展示的条目数)
        PageHelper.startPage(current,3);
        //执行查询
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameLike("%"+name+"%");
        List<User> userList = userMapper.selectByExample(userExample);
        //封装PageInfo对象
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }
}
